from pydantic import BaseModel, Field
import time
import pprint


def time_decorator(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()  # 记录开始时间
        result = func(*args, **kwargs)  # 执行函数
        end_time = time.time()  # 记录结束时间
        d = round(end_time - start_time, 2)
        print(f"函数 {func.__name__} 耗时: {d} 秒")
        return result

    return wrapper


def get_meta_info(cls):
    # AttributeError: 'FieldInfo' object has no attribute 'name'
    def is_pydantic_class(cls):
        return hasattr(cls, '__fields__')

    res = {}
    other_cls = []
    for k in cls.__fields__.keys():

        annotation = cls.__fields__[k].annotation
        is_pyd = is_pydantic_class(annotation)
        if is_pyd:
            other_cls.append(annotation)
            print('pyd', annotation)
        # print('cls===',type(annotation),)
        # print(cls.__fields__[k].annotation  )
        res[k] = cls.__fields__[k]
    return res, other_cls
    # return {
    #     "fields": {
    #         k: cls.__fields__[k]
    #         for k in cls.__fields__.keys()
    #     }
    # }


def gen_prompt(cls, text):
    infos = []
    other_cls = [cls]
    while len(other_cls) > 0:
        cur_cls = other_cls.pop()
        meta_info, new_cls = get_meta_info(cur_cls)
        # print('new_cls',new_cls)
        cur_prompt = "以下是class {}的各个属性的类型信息\n:{}\n".format(cur_cls.__name__, meta_info)
        infos.append(cur_prompt)
        other_cls.extend(new_cls)

    tmp = '\n'.join(infos)
    tmp += '请你根据<cls>的类型信息，以json格式从文本中提取出<cls>,文本内容如下：\n' + text
    return tmp.replace('<cls>', cls.__name__)


class Address(BaseModel):
    """
    地址模型类，用于表示用户地址信息。
    """
    street: str = Field(..., description="街道地址")
    city: str = Field(..., description="城市名称")
    state: str = Field(..., description="州或省名称")
    country: str = Field(..., description="国家名称")


class Person(BaseModel):
    """
    用户模型类，用于表示网站用户信息。
    """

    full_name: str = Field(None, description="用户的全名")
    gender: str = Field(None, description="性别")
    address: Address = Field(None, description="用户的地址信息")


class EducationExperience(BaseModel):
    """
教育经历模型类，用于表示用户的学历信息。
"""
    degree: str = Field(..., description="学历")
    major: str = Field(..., description="专业")
    school: str = Field(..., description="学校")
    start_date: str = Field(..., description="开始日期")
    end_date: str = Field(..., description="结束日期")


from typing import List


class Candidate(BaseModel):
    name: str = Field(..., description="候选人姓名")
    age: int = Field(..., description="候选人年龄")
    gender: str = Field(..., description="候选人性别")
    address: Address = Field(..., description="候选人地址")
    wage_expectation: float = Field(..., description="候选人期望工资")
    eduction: List[EducationExperience] = Field(..., description="候选人教育背景")


from dashscope import get_response
from openai_wenwen import chatgpt_single


@time_decorator
def run():
    text = '马丁，男，28岁，上海，期望工资20k，本科专业是计算机科学与技术，2018年9月到2022年10月在上海交通大学 ，硕士专业是人工智能，2022年10月到2023年11月在帝国理工大学 '
    q = gen_prompt(Candidate, text)
    print(chatgpt_single(q))


# pprint.pprint("以下是class Persion的信息\n:{}".format(get_meta_info(User)))

run()
